/*
 * $Id: LocationImportController.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.controller;

import com.sci.logisticsmap.service.model.GeodataResponse;
import com.sci.logisticsmap.service.model.SessionUserDTModel;
import com.sci.logisticsmap.support.model.ResponseWrapper;
import com.sci.logisticsmap.service.support.LocationImportHelper;
import com.sci.logisticsmap.web.security.support.SecurityContextHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * The Controller for processing Location importing view/ajax requests.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
@Controller
@RequestMapping("")
public class LocationImportController extends BaseController {
    /**
     * The "upload-import-file" ajax path.
     */
    public static final String PATH_AJAX_UPLOAD_IMPORT_FILE = "/ajax/upload-import-file";

    /**
     * The Location import helper.
     */
    @Autowired
    private LocationImportHelper locationImportHelper;

    /**
     * The default constructor.
     * Initializes a Controller class.
     */
    public LocationImportController() {
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseController#init()
     */
    @PostConstruct
    public void init() {
        super.init();
    }

    /**
     * The method to response on the "upload-import-file" ajax request.
     *
     * @param  request
     *         The http servlet request.
     * @param  response
     *         The http servlet response.
     * @param  model
     *         The model.
     */
    @RequestMapping(value=PATH_AJAX_UPLOAD_IMPORT_FILE, method={RequestMethod.POST})
    //public @ResponseBody ResponseWrapper<GeodataResponse>
    public void onUploadImportFile(HttpServletRequest request, HttpServletResponse response, Model model) {

        //String geodataId = null;
        //String errorMessage = null;
        ResponseWrapper<GeodataResponse> responseWrapper;
		try {
            SessionUserDTModel sessionUser = SecurityContextHelper.getAuthenticatedUserDTModel();

            //geodataId = locationImportHelper.importLocations(sessionUser, request);
            responseWrapper = locationImportHelper.importLocations(sessionUser, request);
		} catch (Exception e) {
            responseWrapper = new ResponseWrapper<GeodataResponse>();
            addExceptionToResponseWrapper(responseWrapper, e);
		}

        //MediaType textMediaType = MediaType.TEXT_PLAIN;
        writeResponseUsingCustomMediaType(responseWrapper, MediaType.TEXT_PLAIN, response);

        //return responseWrapper;
    }
}
